/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import gui.utilities.Rater;
import core.exceptions.DBAccessException;
import core.objects.BookComment;
import core.objects.User;
import db.DBAccessEngine;

public class JPanelBookComment extends javax.swing.JPanel {

    private BookComment comment;
    private User user;
    /**
     * Creates new form UserComment
     * @param comment
     */
    public JPanelBookComment(BookComment comment) {
        try {
            this.user = (User) DBAccessEngine.User().select(comment.getCommenterId()).get(0);
            this.comment = comment;
            initComponents();
            initAuxComponent();
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
     * code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelUserName = new javax.swing.JLabel();
        jTextAreaUserComment = new javax.swing.JTextArea();
        jPanelRating = new javax.swing.JPanel();
        jLabelTimestamp = new javax.swing.JLabel();

        setBackground(new java.awt.Color(51, 153, 255));
        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setOpaque(false);

        jLabelUserName.setFont(new java.awt.Font("Trebuchet MS", 1, 13)); // NOI18N
        jLabelUserName.setForeground(new java.awt.Color(153, 255, 153));
        jLabelUserName.setText("Username");
        jLabelUserName.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelUserNameMouseClicked(evt);
            }
        });

        jTextAreaUserComment.setBackground(new java.awt.Color(51, 153, 255));
        jTextAreaUserComment.setColumns(20);
        jTextAreaUserComment.setFont(new java.awt.Font("Trebuchet MS", 3, 13)); // NOI18N
        jTextAreaUserComment.setForeground(new java.awt.Color(153, 255, 153));
        jTextAreaUserComment.setLineWrap(true);
        jTextAreaUserComment.setRows(5);
        jTextAreaUserComment.setWrapStyleWord(true);
        jTextAreaUserComment.setAlignmentX(0.0F);
        jTextAreaUserComment.setAlignmentY(0.0F);
        jTextAreaUserComment.setAutoscrolls(false);
        jTextAreaUserComment.setBorder(null);
        jTextAreaUserComment.setFocusable(false);

        jPanelRating.setAlignmentX(0.0F);
        jPanelRating.setAlignmentY(0.0F);
        jPanelRating.setMaximumSize(new java.awt.Dimension(90, 45));
        jPanelRating.setOpaque(false);
        jPanelRating.setPreferredSize(new java.awt.Dimension(90, 45));
        jPanelRating.setLayout(new java.awt.BorderLayout());

        jLabelTimestamp.setFont(new java.awt.Font("Trebuchet MS", 1, 13)); // NOI18N
        jLabelTimestamp.setForeground(new java.awt.Color(153, 255, 153));
        jLabelTimestamp.setText("Time");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabelTimestamp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextAreaUserComment)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextAreaUserComment, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelUserName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelTimestamp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jLabelUserNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelUserNameMouseClicked
        MainForm.searchForUsers(user);
    }//GEN-LAST:event_jLabelUserNameMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelTimestamp;
    private javax.swing.JLabel jLabelUserName;
    private javax.swing.JPanel jPanelRating;
    private javax.swing.JTextArea jTextAreaUserComment;
    // End of variables declaration//GEN-END:variables

    private void initAuxComponent() {
       jLabelUserName.setText(user.getName()+" said: ");
       jTextAreaUserComment.setText(comment.getComment());
       Rater rater = new Rater(comment.getRating(), -1);
       rater.setEnabled(false);
       jPanelRating.add(rater);
       jLabelTimestamp.setText(GUIUtils.removeSecondsFromTimestamp(comment.getTimestamp()));
       
    }
}
